package com.example.spsaveqqty.userManage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.spsaveqqty.PersonalGuideDetailActivity;
import com.example.spsaveqqty.R;
import com.example.spsaveqqty.guides.Guide;

import java.util.List;

public class guidesAdapter extends BaseAdapter {

    private List<Guide> list;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public guidesAdapter(List<Guide> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        if (list.size() % 2 > 0) {
            return list.size() / 2 + 1;
        } else {
            return list.size() / 2;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.guides_list_item, null);
            vh.img1 = convertView.findViewById(R.id.img_guides);
            vh.title1 = convertView.findViewById(R.id.tv_guide_title);
            vh.place1 = convertView.findViewById(R.id.tv_guide_place_name);
            vh.time1 = convertView.findViewById(R.id.tv_guide_time);
            vh.img2 = convertView.findViewById(R.id.img_guides2);
            vh.title2 = convertView.findViewById(R.id.tv_guide_title2);
            vh.place2 = convertView.findViewById(R.id.tv_guide_place_name2);
            vh.time2 = convertView.findViewById(R.id.tv_guide_time2);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder)convertView.getTag();
        }
        int distance =  list.size() - position*2;
        int cellCount = distance >= 2? 2:distance;
        final List<Guide> itemList = list.subList(position*2,position*2+cellCount);

        if (itemList.size() >0) {//表示有数据，先给第一列赋值
            if (!TextUtils.isEmpty(itemList.get(0).getPicture()[0])) {
                Bitmap bitmap = BitmapFactory.decodeFile(itemList.get(0).getPicture()[0]);
                bitmap = Bitmap.createScaledBitmap(bitmap,168,215,true);
                vh.img1.setImageBitmap(bitmap);
            }
            //给图片加上点击事件：
            vh.img1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, PersonalGuideDetailActivity.class);
                    intent.putExtra("guide",itemList.get(0));
                    mContext.startActivity(intent);
                }
            });
            vh.title1.setText(itemList.get(0).getTitle());
            vh.place1.setText(itemList.get(0).getPlace_name());
            vh.time1.setText(itemList.get(0).getDatetime());
            if (itemList.size() == 1){//表示只有一列，则设置第二列不可见
                vh.img2.setVisibility(View.INVISIBLE);
                vh.title2.setVisibility(View.INVISIBLE);
                vh.place2.setVisibility(View.INVISIBLE);
                vh.time2.setVisibility(View.INVISIBLE);
            }
            if (itemList.size() >1){//表示不止1列，则设置第二列可见，并给第二列赋值
                vh.img2.setVisibility(View.VISIBLE);
                vh.title2.setVisibility(View.VISIBLE);
                vh.place2.setVisibility(View.VISIBLE);
                vh.time2.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(itemList.get(1).getPicture()[0])){
                    Bitmap bitmap2 = BitmapFactory.decodeFile(itemList.get(1).getPicture()[0]);
                    bitmap2 = Bitmap.createScaledBitmap(bitmap2,168,215,true);
                    vh.img2.setImageBitmap(bitmap2);
                }
                //给图片加上点击事件：
                vh.img2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, PersonalGuideDetailActivity.class);
                        intent.putExtra("guide",itemList.get(1));
                        mContext.startActivity(intent);
                    }
                });
                vh.title2.setText(itemList.get(1).getTitle());
                vh.place2.setText(itemList.get(1).getPlace_name());
                vh.time2.setText(itemList.get(1).getDatetime());
            }
        }else{//没有值 均设置为不可见
            vh.img1.setVisibility(View.INVISIBLE);
            vh.title1.setVisibility(View.INVISIBLE);
            vh.place1.setVisibility(View.INVISIBLE);
            vh.time1.setVisibility(View.INVISIBLE);
            vh.img2.setVisibility(View.INVISIBLE);
            vh.title2.setVisibility(View.INVISIBLE);
            vh.place2.setVisibility(View.INVISIBLE);
            vh.time2.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    /**
     * 封装ListView中item控件以优化ListView
     */
    public static class ViewHolder {

        ImageView img1;
        TextView title1;
        TextView place1;
        TextView time1;

        ImageView img2;
        TextView title2;
        TextView place2;
        TextView time2;
    }

}
